#ifndef _CONFIG_H
#define _CONFIG_H

#include <stdio.h>
#include "stm32f4xx.h"

/**
 * @brief camera size
 *  
 */
#define frame_crop_width 251 //176
#define frame_crop_height 100 //144
#define frame_width 251 //176
#define frame_height 100 //144
#define frame_size frame_width*frame_height*2
    /*
  D0 -- PC6
  D1 -- PC7
  D2 -- PE0
  D3 -- PE1
  D4 -- PE4
  D5 -- PB6
  D6 -- PB8
  D7 -- PB9
  PCK - PA6
  HS -- PA4
  VS -- PB7
  XCLK -- PA8

  dma2 stream1
  */

/**
 * @brief Definition for COM port1, connected to USART
 * this is used for debugging output 
 */ 
#define Debug_USART                        USART3
#define Debug_USART_CLK                    RCC_APB1Periph_USART3

#define Debug_USART_BAUD				   115200

#define Debug_USART_TX_PIN                 GPIO_Pin_10
#define Debug_USART_TX_GPIO_PORT           GPIOC
#define Debug_USART_TX_GPIO_CLK            RCC_AHB1Periph_GPIOC
#define Debug_USART_TX_SOURCE              GPIO_PinSource10
#define Debug_USART_TX_AF                  GPIO_AF_USART3

#define Debug_USART_RX_PIN                 GPIO_Pin_11
#define Debug_USART_RX_GPIO_PORT           GPIOC
#define Debug_USART_RX_GPIO_CLK            RCC_AHB1Periph_GPIOC
#define Debug_USART_RX_SOURCE              GPIO_PinSource11
#define Debug_USART_RX_AF                  GPIO_AF_USART3

#define Debug_USART_IRQn                   USART3_IRQn

/**
 * @brief Definition communication port
 */
 // todo should we change this to  pd5/6 and use tim2 ch3/4 ?
#define zbCom_USART                        USART2
#define zbCom_USART_BAUD				   921600
#define zbCom_DRADDR					   (uint32_t)&(USART2->DR)
#define zbCom_USART_CLK                    RCC_APB1Periph_USART2
// CAREFULL THIS IS ALSO PWDWN ON CAM
#define zbCom_USART_TX_PIN                 GPIO_Pin_2
#define zbCom_USART_TX_GPIO_PORT           GPIOA
#define zbCom_USART_TX_GPIO_CLK            RCC_AHB1Periph_GPIOA
#define zbCom_USART_TX_SOURCE              GPIO_PinSource2
#define zbCom_USART_TX_AF                  GPIO_AF_USART2

#define zbCom_USART_RX_PIN                 	GPIO_Pin_3
#define zbCom_USART_RX_GPIO_PORT           	GPIOA
#define zbCom_USART_RX_GPIO_CLK            	RCC_AHB1Periph_GPIOA
#define zbCom_USART_RX_SOURCE              	GPIO_PinSource3
#define zbCom_USART_RX_AF                  	GPIO_AF_USART2
// dma, note they cant be anything see pg.216 in 31020
#define zbCom_USART_IRQn                   	USART2_IRQn
#define zbCom_DMA_STREAM_IRQ           	   	DMA1_Stream6_IRQn
#define zbCom_DMA_CHANNEL					DMA_Channel_4
#define zbCom_Usart_DMA_Stream				DMA1_Stream6
#define zbCom_DMA_FLAG_TC					DMA_FLAG_TCIF6
#define zbCom_Usart_DMA_CLK					RCC_AHB1Periph_DMA1

/**
 * @brief PWM OUT port (motor left and right)
 * PB4,PB5,PC0,PC1
 */
#define ZPWMO_GPIO_CLKS			RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOD 
#define ZPWMO_CLK_TIM			RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4
#define ZPWMO_PERIOD			665	  // TODO fix period so user can use
#define ZPWM0_TIM				TIM3
#define ZPWM0_TIM_CLK			RCC_APB1Periph_TIM3
#define ZPWM0_TIM2				TIM4
#define ZPWM0_TIM_CLK2			RCC_APB1Periph_TIM4
#define ZPWM0_FQ				1000
// left motor ?
#define ZPWMO_PORT1				GPIOB
#define ZPWMO_PINSOURCE1		GPIO_PinSource4 
#define ZPWMO_PINSOURCE2		GPIO_PinSource5 
#define ZPWMO_PIN1				GPIO_Pin_4
#define ZPWMO_PIN1_CCR			TIM3->CCR1
#define ZPWMO_PIN2				GPIO_Pin_5
#define ZPWMO_PIN2_CCR			TIM3->CCR2

// right motor ? 
#define ZPWMO_PORT2				GPIOB
#define ZPWMO_PINSOURCE3		GPIO_PinSource0
#define ZPWMO_PINSOURCE4		GPIO_PinSource1
#define ZPWMO_PIN3				GPIO_Pin_0
#define ZPWMO_PIN3_CCR			TIM3->CCR3
#define ZPWMO_PIN4				GPIO_Pin_1
#define ZPWMO_PIN4_CCR			TIM3->CCR4

// cam motor
#define ZPWMO_PORT3				GPIOD
#define ZPWMO_PINSOURCE5		GPIO_PinSource12 
#define ZPWMO_PINSOURCE6		GPIO_PinSource13
#define ZPWMO_PIN5				GPIO_Pin_12
#define ZPWMO_PIN5_CCR			TIM4->CCR1 // HARD CODED !! 
#define ZPWMO_PIN6				GPIO_Pin_13
#define ZPWMO_PIN6_CCR			TIM4->CCR2 // HARD CODED !!

// other motor ?
#define ZPWMO_PORT4				GPIOD
#define ZPWMO_PINSOURCE7		GPIO_PinSource14 
#define ZPWMO_PINSOURCE8		GPIO_PinSource15
#define ZPWMO_PIN7				GPIO_Pin_14
#define ZPWMO_PIN7_CCR			TIM4->CCR3 // HARD CODED !!
#define ZPWMO_PIN8				GPIO_Pin_15
#define ZPWMO_PIN8_CCR			TIM4->CCR4 // HARD CODED !!

#define ZPWM0_AF				GPIO_AF_TIM3
#define ZPWM0_AF2				GPIO_AF_TIM4

/**
 * gpio perph defines
 */
#define ZGPIO_READ_PINS 		GPIO_Pin_7 | GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11| GPIO_Pin_12| GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15
#define ZGPIO_READ_PORT			GPIOE
#define ZGPIO_READ_DR			GPIOE->IDR
#define ZGPIO_READ_CLK			RCC_AHB1Periph_GPIOE
#define ZGPIO_READ_MASK			0x00

#define ZGPIO_WRITE_PINS 		GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2| GPIO_Pin_3| GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6| GPIO_Pin_7
#define ZGPIO_WRITE_PORT		GPIOD
#define ZGPIO_WRITE_CLK			RCC_AHB1Periph_GPIOD
#define ZGPIO_WRITE_MASK		0xFFFF




/**
 * ADC perph defines
 */
#define ZANAL_ADC				 ADC3
#define ZANAL_DR				 ((uint32_t)&ADC3->DR)
#define ZANAL_VALUE				 ADC3->DR
#define ZANAL_ADCCLK   			 RCC_APB2Periph_ADC3
#define ZANAL_PIN1				 GPIO_Pin_1
#define ZANAL_PIN2				 GPIO_Pin_2
#define ZANAL_PIN3				 GPIO_Pin_3
#define ZANAL_PIN1_PORT			 GPIOC
#define ZANAL_GPIO1CLK			 RCC_AHB1Periph_GPIOC
#define ZANAL_ADCCHAN1			 ADC_Channel_11
#define ZANAL_ADCCHAN2			 ADC_Channel_12
#define ZANAL_ADCCHAN3			 ADC_Channel_13
#define ZANAL_DMA_CHAN			 DMA_Channel_2
#define ZANAL_DMA_CLK			 RCC_AHB1Periph_DMA2
#define ZANAL_DMA_STREAM  		 DMA2_Stream0


/* DCMI Communication boards Interface */

/**
 * @connected to I2C2, used for camera sccb init 
 */
 /* Configure I2C1 pins: PB10->SIOC and PB11->SIOD */ 
#define Open207V_SCCB                        	I2C2
#define Open207V_SCCB_CLK                    	RCC_APB1Periph_I2C2

#define Open207V_SCCB_SDA_PIN                 	GPIO_Pin_11
#define Open207V_SCCB_SDA_GPIO_PORT           	GPIOB
#define Open207V_SCCB_SDA_GPIO_CLK            	RCC_AHB1Periph_GPIOB
#define Open207V_SCCB_SDA_SOURCE              	GPIO_PinSource10
#define Open207V_SCCB_SDA_AF                  	GPIO_AF_I2C2

#define Open207V_SCCB_SCL_PIN                 	GPIO_Pin_10
#define Open207V_SCCB_SCL_GPIO_PORT           	GPIOB
#define Open207V_SCCB_SCL_GPIO_CLK            	RCC_AHB1Periph_GPIOB
#define Open207V_SCCB_SCL_SOURCE              	GPIO_PinSource11
#define Open207V_SCCB_SCL_AF                  	GPIO_AF_I2C2

#endif	  /*_CONFIG_H*/

